<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>

      <a-form-item
        label="编码"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-decorator="['no', {rules: [{required: false, message: '编码不能为空'}]}]" />
      </a-form-item>

<!--      <a-form-item
        label="所属公司"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select @change="handleCompanyChange" v-decorator="['useCompany', {rules: [{required: true, message: '所属公司不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="({deptId, name}) in companyList"
            :key="deptId"
            :label="name"
            :value="deptId">{{ name }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="所属项目部"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select @change="handleProjectChange" v-decorator="['useProject']" placeholder="请选择">
          <a-select-option
            v-for="({deptId, name}) in projectList"
            :key="deptId"
            :label="name"
            :value="deptId">{{ name }}
          </a-select-option>
        </a-select>
      </a-form-item>-->
      <a-form-item
        label="所属仓库"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-tree-select
          style="width: 100%"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="storeTreeDate"
          :treeNodeFilterProp="'title'"
          :showSearch="true"
          v-decorator="['parentId', {rules: [{required: false, message: '上层仓库不能为空'}]}]"
          placeholder="请选择"
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item
        label="名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['name', {rules: [{required: true, message: '名称不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="仓库负责人"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['userId', {rules: [{required: true, message: '仓库负责人不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="({userId, realName}) in userList"
            :key="userId"
            :label="realName"
            :value="userId">{{ realName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="等級"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['level']" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in levelMap"
            :key="value"
            :label="label"
            :value="parseInt(value)">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>
<!--      <a-form-item
        label="类型"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['type']" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in typeMap"
            :key="value"
            :label="label"
            :value="parseInt(value)">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>-->
      <a-form-item
        label="排序"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="1"
          v-decorator="['sort', {initialValue:1,rules: [{required: true, message: '排序不能为空'}]}]" />
      </a-form-item>
<!--      <a-form-item
        label="是否删除"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['delFlag', {initialValue:DictCache.VALUE.DELFLAG.NORMAL,rules: [{required: true, message: '是否删除不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in delFlagMap"
            :key="value"
            :label="label"
            :value="parseInt(value)">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>-->
      <a-form-item
        label="备注"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-textarea
          :rows="4"
          v-decorator="['remark', {rules: [{required: false, message: '备注不能为空'}]}]"/>
      </a-form-item>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addStore, updateStore, fetchStoreTree } from '@/api/store/store'
import { queryDept, getDeptsAllByParentId } from '@/api/upms/dept'
import { queryUser, queryUsersByParentDeptNatureAll } from '@/api/upms/user'

export default {
  name: 'BaseStore',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      typeMap: {},
      levelMap: {},
      companyList: {},
      projectList: {},
      userList: {},
      delFlagMap: {},
      storeTreeDate: []
    }
  },
  props: {
  },
  created () {
    // 下拉框map
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.STORE_LEVEL)
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.STORE_TYPE)
    this.delFlagMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.DELFLAG)
  },
  methods: {
    base (record) {
      this.setTree(record)
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      if (this.BaseTool.Object.isBlank(record.id)) {
        this.modalTitle = '复制'
      }

      const params = { includeMy: true }
      params.deptId = record.useCompany
      params.nature = this.DictCache.VALUE.SYS_DEPT_NATURE.FEN_GONG_SI
      // params.code = this.DictCache.VALUE.SYS_DEPT_CODE.CANG_KU
      queryUsersByParentDeptNatureAll(params).then(res => {
        this.userList = res.data
      })
      const { form: { setFieldsValue } } = this

      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'useCompany',
          'useProject',
          'userId',
          'level',
          'no',
          'name',
          'type',
          'sort',
          'delFlag',
          'parentId',
          'remark',
          'parentName'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addStore(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateStore(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    },
    setTree (record = {}) {
      fetchStoreTree({ id: record.id }).then(res => {
        this.storeTreeDate = res.data
      })
      queryDept({ nature: this.DictCache.VALUE.SYS_DEPT_NATURE.FEN_GONG_SI }).then(res => {
        this.companyList = res.data
      })
      getDeptsAllByParentId({ deptId: record.useCompany, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.XIANG_MU_BU }).then(res => {
        this.projectList = res.data
      })
      queryUser({ deptId: record.useProject }).then(res => {
        this.userList = res.data
      })
    },
    handleCompanyChange (value) {
      getDeptsAllByParentId({ deptId: value, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.XIANG_MU_BU }).then(res => {
        this.projectList = res.data
      })
    },
    handleProjectChange (value) {
      const params = { includeMy: true }
      if (this.BaseTool.Object.isBlank(value)) {
        this.$message.info('请先选择项目部')
        return
      } else {
        params.deptId = value
      }
      params.nature = this.DictCache.VALUE.SYS_DEPT_NATURE.FEN_GONG_SI
      // params.code = this.DictCache.VALUE.SYS_DEPT_CODE.CANG_KU
      queryUsersByParentDeptNatureAll(params).then(res => {
        this.userList = res.data
      })
      // const { form: { setFieldsValue } } = this
      // this.projectList.forEach(item => {
      //  if (item.deptId === value) {
      //    setFieldsValue({ name: item.name + '-' })
      //  }
      // })
    }

  }
}
</script>
